package com.ruoyi.life.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.life.domain.vo.FeApartmentExcelVO;
import com.ruoyi.life.domain.vo.FeApartmentVO;
import com.ruoyi.life.service.IFeApartmentService;
import com.ruoyi.omp.api.domain.FeApartment;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 公寓租赁记录Controller
 *
 * @author cjie
 * @date 2021-09-17
 */
@Api(tags = " 公寓租赁")
@RestController
@RequestMapping("/feApartment")
public class FeApartmentController extends BaseController {
    @Autowired
    private IFeApartmentService feApartmentService;

    /**
     * 查询公寓租赁记录列表
     */
    @ApiOperation(value="查询公寓租赁记录列表", notes="查询公寓租赁记录列表")
    @PreAuthorize(hasPermi = "life:feApartment:list")
    @GetMapping("/list")
    public TableDataInfo list(FeApartment feApartment) {
        startPage();
        List<FeApartment> list = feApartmentService.selectFeApartmentList(feApartment);
        return getDataTable(list);
    }

    /**
     * 导出公寓租赁记录列表
     */
    @ApiOperation(value="导出公寓租赁记录列表", notes="导出公寓租赁记录列表")
    @PreAuthorize(hasPermi = "life:feApartment:export")
    @Log(title = "公寓租赁记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FeApartment feApartment) throws IOException {
        List<FeApartment> list = feApartmentService.selectFeApartmentList(feApartment);
        ExcelUtil<FeApartment> util = new ExcelUtil<FeApartment>(FeApartment.class);
        util.exportExcel(response, list, "公寓租赁记录数据");
    }
    /**
     * 获取公寓租赁记录详细信息
     */
    @ApiOperation(value="获取公寓租赁记录详细信息", notes="获取公寓租赁记录详细信息")
    @PreAuthorize(hasPermi = "life:feApartment:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(feApartmentService.selectFeApartmentById(id));
    }

    @ApiOperation(value="WX-公寓租赁企业列表", notes="WX-公寓租赁企业列表")
    @GetMapping(value = "/leaseEnList")
    public AjaxResult getLeaseEn() {
        return AjaxResult.success(feApartmentService.selectFeApartmentEnList());
    }


    /**
     * 新增公寓租赁记录
     */
    @ApiOperation(value="新增公寓租赁记录", notes="新增公寓租赁记录")
    @PreAuthorize(hasPermi = "life:feApartment:add")
    @Log(title = "公寓租赁记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FeApartment feApartment) {
        return toAjax(feApartmentService.insertFeApartment(feApartment));
    }

    /**
     * 新增公寓租赁记录
     */
    @ApiOperation(value="企业入驻-租赁公寓", notes="企业入驻-租赁公寓")
    @PreAuthorize(hasPermi = "life:feApartment:add")
    @Log(title = "公寓租赁记录", businessType = BusinessType.INSERT)
    @PostMapping("/addFeApartment")
    public AjaxResult addFeApartment(@RequestBody FeApartmentVO feApartmentVO) {
        return toAjax(feApartmentService.addFeApartment(feApartmentVO));
    }

    @ApiOperation(value="WX-获取公寓租赁记录详细信息", notes="WX-获取公寓租赁记录详细信息")
    @PreAuthorize(hasPermi = "life:feApartment:query")
    @GetMapping(value = "/getVO/{id}")
    public AjaxResult getVOInfo(@PathVariable("id") String id) {
        return AjaxResult.success(feApartmentService.selectFeApartmentVOById(id));
    }

    /**
     * 修改公寓租赁记录
     */
    @ApiOperation(value="修改公寓租赁记录", notes="修改公寓租赁记录")
    @PreAuthorize(hasPermi = "life:feApartment:edit")
    @Log(title = "公寓租赁记录", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/updateFeApartment")
    public AjaxResult updateFeApartment(@RequestBody FeApartmentVO feApartmentVO) {
        return toAjax(feApartmentService.updateFeApartment1(feApartmentVO));
    }

    /**
     * 修改公寓租赁记录
     */
    @ApiOperation(value="修改公寓租赁记录", notes="修改公寓租赁记录")
    @PreAuthorize(hasPermi = "life:feApartment:edit")
    @Log(title = "公寓租赁记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FeApartment feApartment) {
        return toAjax(feApartmentService.updateFeApartment(feApartment));
    }

    /**
     * 删除公寓租赁记录
     */
    @ApiOperation(value="删除公寓租赁记录", notes="删除公寓租赁记录")
    @ApiImplicitParam(name = "ids", value = "公寓租赁记录ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "life:feApartment:remove")
    @Log(title = "公寓租赁记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(feApartmentService.deleteFeApartmentByIds(ids));
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<FeApartmentExcelVO> util = new ExcelUtil<FeApartmentExcelVO>(FeApartmentExcelVO.class);
        util.importTemplateExcel(response, "仪表数据");
    }

    /**
     * 导入抄表记录列表,
     */
    @PreAuthorize(hasPermi = "life:feApartment:import")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, String meterType) throws Exception {
        ExcelUtil<FeApartmentExcelVO> util = new ExcelUtil<FeApartmentExcelVO>(FeApartmentExcelVO.class);
        List<FeApartmentExcelVO> buiRoomVOList = util.importExcel(file.getInputStream());
        //批量插入数据
        String message = feApartmentService.importData(buiRoomVOList,meterType);
        return AjaxResult.success(message);
    }

}
